﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using ZoeEngine.Framework;
using ZoeEngine.Components.Physics;
using ZoeEngine.Services.Input;

namespace ZoeEngine.Components.Controllers
{
    [ComponentDependency(typeof(PhysWheelJoint), typeof(PhysBodyComponent))]
    public class PlatformerKeyboardController : BaseKeyboardComponent
    {
        private IInputService _inputService;

        private PhysWheelJoint _playerMotor;
        private PhysBodyComponent _playerBody;

        public override void Initialize()
        {
            base.Initialize();
            _playerBody = this.Owner.Find<PhysBodyComponent>();
            _playerMotor = this.Owner.Find<PhysWheelJoint>();
        }
        public override void Start()
        {
            base.Start();

            _inputService = Zoe.GetService<IInputService>();
        }

        protected override void OnKeyboardKeyDown(object sender, KeyboardEventArgs e)
        {
            base.OnKeyboardKeyDown(sender, e);

            switch (e.ActiveKey)
            {
                case Keys.D:
                    _playerMotor.MotorEnabled = true;
                    break;
                case Keys.A:
                    _playerMotor.MotorEnabled = true;
                    break;
                default:
                    break;
            }
        }
        protected override void OnKeyboardKeyHeld(object sender, KeyboardEventArgs e)
        {
            base.OnKeyboardKeyHeld(sender, e);

            switch (e.ActiveKey)
            {
                case Keys.D:
                    _playerMotor.MotorSpeed += 1.0f;
                    break;
                case Keys.A:
                    _playerMotor.MotorSpeed -= 1.0f;
                    break;
                default:
                    break;
            }
            MathHelper.Clamp(_playerMotor.MotorSpeed, -10.0f, 10.0f);
        }
        protected override void OnKeyboardKeyUp(object sender, KeyboardEventArgs e)
        {
            base.OnKeyboardKeyUp(sender, e);

            switch (e.ActiveKey)
            {
                case Keys.A:
                case Keys.D:
                    _playerMotor.MotorEnabled = false;
                    break;
                default:
                    break;
            }
        }
    }
}
